﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_LineProcecureController : BaseApiController
    {
        #region DI

        public PB_LineProcecureController(IPB_LineProcecureBusiness pB_LineProcecureBus)
        {
            _pB_LineProcecureBus = pB_LineProcecureBus;
        }

        IPB_LineProcecureBusiness _pB_LineProcecureBus { get; }

        #endregion

        #region 获取
        [HttpPost]
        public async Task<PageResult<PB_LineProcecure>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_LineProcecureBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_LineProcecure> GetTheData(IdInputDTO input)
        {
            return await _pB_LineProcecureBus.GetTheDataAsync(input.id);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_LineProcecureDTO data)
        {
            //先删除原有的lineid的所有数据
            var where = LinqHelper.True<PB_LineProcecure>();
            if (!data.LineId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_LineProcecure, bool>(
                        ParsingConfig.Default, false, $@"LineId.Equals(@0)", data.LineId);
                where = where.And(newWhere);
            }
            await _pB_LineProcecureBus.DeleteOtherIdDataAsync(where);
            //插入新的

            if (data.Id.IsNullOrEmpty())
            {
                foreach (var item in data.ProcecureId)
                {
                    InitEntity(data);
                    PB_LineProcecure LineProcecure = new PB_LineProcecure();
                    LineProcecure.Id = data.Id;
                    LineProcecure.LineId = data.LineId;
                    LineProcecure.ProcecureId = item.ToString();
                    await _pB_LineProcecureBus.AddDataAsync(LineProcecure);
                }

            }
            else
            {
                List<string> ListString = new List<string>();
                ListString.Add(data.Id);
                await _pB_LineProcecureBus.DeleteDataAsync(ListString);
                foreach (var item in data.ProcecureId)
                {
                    PB_LineProcecure LineProcecure = new PB_LineProcecure();
                    LineProcecure.Id = data.Id;
                    LineProcecure.LineId = data.LineId;
                    LineProcecure.ProcecureId = item.ToString();
                    await _pB_LineProcecureBus.UpdateDataAsync(LineProcecure);
                }
            }
        }
        /// <summary>
        /// 根据产线管理lineid查询 PB_LineProcecure 的信息
        /// </summary>
        [HttpPost]
        public async Task<List<PB_LineProcecureSelect>> GetDataListByArea(string LineId)
        {
            var res = await _pB_LineProcecureBus.GetDataListAsync(LineId);
            return res;
        }
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_LineProcecureBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}